package org.blame.rabbitmqspringboot.controller;


import org.blame.rabbitmqspringboot.constants.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rabbit")
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/work")
    public String work() {

        for (int i = 0; i < 10; i++) {
            System.out.println("消息发送成功");
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE123, "hello rabbitMQ Spring Boot");
        }
        return "消息发送成功";
    }

    @RequestMapping("/fanout")
    public String fanout() {
        for (int i = 0; i < 10; i++) {
            System.out.println("消息发送成功");
            rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE, "", "hello spring amqp:fanout...");
        }
        return "发送成功";

    }

    @RequestMapping("/directByOrange")
    public String directByOrange() {
        for (int i = 0; i < 4; i++) {
            System.out.println("消息发送成功");
            rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE, "orange", "<orange>hello spring amqp:direct...");
        }
        return "发送成功";

    }

    @RequestMapping("/directByBlack")
    public String directByBlack() {
        for (int i = 0; i < 4; i++) {
            System.out.println("消息发送成功");
            rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE, "black", "<black>hello spring amqp:direct...");
        }
        return "发送成功";
    }

    @RequestMapping("/topic/{routingKey}")
    public String topic(@PathVariable("routingKey") String routingKey){
        System.out.println("消息发送成功");
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE, routingKey, "hello spring amqp:topic, my routing key is "+routingKey);
        return "发送成功";
    }


}
